package com.macro.mall.tiny.modules.ums.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macro.mall.tiny.common.api.CommonPage;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.modules.ums.dto.UmsAdminParam;
import com.macro.mall.tiny.modules.ums.dto.UmsAdminScoreInfo;
import com.macro.mall.tiny.modules.ums.model.Group;
import com.macro.mall.tiny.modules.ums.model.UmsAdmin;
import com.macro.mall.tiny.modules.ums.model.UmsResource;
import com.macro.mall.tiny.modules.ums.service.GroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@Api(tags = "GroupController", description = "小组管理")
@RequestMapping("/group")
public class GroupController {


    @Autowired
    private GroupService groupService;


    @ApiOperation("获取小组名")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<Group>> list() {
        List<Group> groupName = groupService.listGroupName();
        return CommonResult.success((groupName));
    }


    @ApiOperation(value = "")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Integer> update(@Validated @RequestBody List<Group> group) {
        int status = groupService.updateRation(group);
        if (status < 0) {
            return CommonResult.failed();
        }
        return CommonResult.success(status);
    }


    @ApiOperation(value = "新增小组")
    @RequestMapping(value = "/saveGroup", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Group> saveGroup(@Validated @RequestBody Group group) {
        Group dbGroup = groupService.saveGroup(group);
        if (dbGroup == null) {
            return CommonResult.failed();
        }
        return CommonResult.success(dbGroup);
    }

    @ApiOperation("获取当前登陆用户小组名称")
    @RequestMapping(value = "/getGroup", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Group> getGroup(@RequestParam(value = "username", required = true) String username) {
        Group groupName = groupService.getGroup(username);
        return CommonResult.success((groupName));
    }




    @ApiOperation("删除指定名称小组")
    @RequestMapping(value = "/deleteGroupByName/{name}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@PathVariable String name) {
        boolean success = groupService.deleteGroupByName(name);
        if (success) {
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }

}
